package com.xiaodu.springboot.controller;

import com.xiaodu.springboot.entity.Product;
import com.xiaodu.springboot.ex.BusinessException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.hibernate.validator.constraints.Length;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * @author: dutianjie
 * @date: 2021/3/31
 * @version: v1.0.0
 * @description: 描述
 **/
@RestController
@RequestMapping("validator")
@Api(tags = "商品操作")
@Validated
public class ProductController {

    public static final Logger log = LoggerFactory.getLogger(ProductController.class);

    @ApiOperation(value = "获取商品详情", httpMethod = "GET")
    @GetMapping("product")
    public Product getProduct(@Length(max = 32, min = 32) @RequestParam String id) throws Exception {
        Product product = new Product();
        product.setId(id);
        product.setName("xiaomi 11");
        product.setPrice(199900L);
        return product;
    }

    @ApiOperation(value = "添加商品信息", httpMethod = "POST")
    @PostMapping("product")
    public String addProduct(@Validated @RequestBody Product product) throws Exception {
        return product.getId();
    }

    @ApiOperation(value = "添加商品信息", httpMethod = "POST")
    @PostMapping("product2")
    public String addProduct2(@Validated Product product) throws Exception {
        return product.getId();
    }

    @GetMapping("ex")
    @ApiOperation(value = "测试自定义异常处理", httpMethod = "GET")
    public Object testMyException(@RequestParam(required = false) @ApiParam(name = "str", value = "str") String str) throws Exception {
        throw new BusinessException("test businessException", 5001);
    }

    @GetMapping("ex2")
    @ApiOperation(value = "测试默认异常处理", httpMethod = "GET")
    public void testDefException() throws Exception {
        int i = 1 / 0; // 模拟程序报错
    }


}
